﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gtk;
using ESF.General.MVP;
using ESF.General;
using ESF.General.GUI;

#endregion


namespace ESF.GTK.Elements
{
	public class BaseGTKDialog<P,I>
		: Dialog, IBaseViewer
		where I : IBaseViewer
		where P : BasePresenter<I>
	{
		#region Members

		protected P _Presenter;

		protected IProgressDialog<Window> ProgressDialog;

		#endregion


		#region Constructor

		public BaseGTKDialog (Window pParent)
			: base (string.Empty, pParent, DialogFlags.Modal) 
		{
			if (string.IsNullOrWhiteSpace(Title))
			{
				Title = ReadConfig.ApplicationName;
			}

			Title += " - versión: " + ReadConfig.ExecutingVersion; 
			
			ProgressDialog = new ProgressDialog();
			ProgressDialog.OnCancel += CancelCurrentOperation;
		}

		#endregion


		#region IBaseViewer Fields

		public string frmUserHostAddress { get { return Log.UserHostAddress; } }


		public string frmErrorMsg
		{
			set
			{
				MessageDialog md = new MessageDialog(this,
					DialogFlags.Modal, MessageType.Error,
					ButtonsType.Close, value);
				md.Run();
				md.Destroy();
			}
		}

		public string frmWarnMsg
		{
			set
			{
				MessageDialog md = new MessageDialog(this,
				  DialogFlags.Modal, MessageType.Warning,
				  ButtonsType.Close, value);
				md.Run();
				md.Destroy();
			}
		}


		public string frmInfoMsg
		{
			set
			{
				MessageDialog md = new MessageDialog(this,
				  DialogFlags.Modal, MessageType.Info,
				  ButtonsType.Ok, value);
				md.Run();
				md.Destroy();
			}
		}

		public bool frmIsAuthenticated
		{
			get { return false; }
		}

		public string frmUserAlias
		{
			get { return string.Empty; }
		}


		public int CurrentAdvance100 { set { ProgressDialog.PercentageAdvance100 = value; } }

		public string CurrentStepName { set { ProgressDialog.CurrentStepName = value; } }

		#endregion


		#region Methods

		protected virtual void CancelCurrentOperation () {}


		protected virtual void Clear () {}


		public void ShowClearProgressDialog ()
		{
			Application.Invoke (delegate 
			{  
				ProgressDialog.Clear ();
				ProgressDialog.Show (this); 
			});
		}


		public int ShowConfirm(string pMessage)
		{
			return GTKHelper.ShowConfirm (this, pMessage);
		}


		public void CloseApplication ()
		{
			GTKHelper.CloseApplication ();
		}


		public new void Focus (Widget pWidget)
		{
			GTKHelper.Focus (pWidget);
		}


		public void SetText(Label pLabel, string pText)
		{
			GTKHelper.SetText (pLabel, pText);
		}


		public void SetFraction(ProgressBar pProgresBar, double pPercentage100)
		{
			GTKHelper.SetFraction (pProgresBar, pPercentage100);
		}


		public void LogError (Exception pEx)
		{
			Application.Invoke (delegate { ProgressDialog.Hide (); });

			Log.Error (pEx);
			
			frmErrorMsg = pEx.Message;
		}


		public void SetAdvance(string pMessage = "", int pTotalWorkCount = 0, int pReadyWorkCount = 0, int pTotalErrors = 0, long pItemElapsedMs = 0)
		{
			double advance100;


			advance100 = ProcessHelper.GetAdvancePercentage100(pTotalWorkCount, pReadyWorkCount + pTotalErrors);

			CurrentStepName =
				(string.IsNullOrEmpty(pMessage) ? string.Empty : pMessage + "... ") +
				string.Format("Procesados {0} de {1}{3}: {2:0.##}%", pReadyWorkCount, pTotalWorkCount, advance100,
				(pTotalErrors > 0 ? string.Format(" con {0} errores", pTotalErrors) : string.Empty));

			CurrentAdvance100 = (int)advance100;
		}


		public void SetAdvanceCompleted()
		{
			CurrentAdvance100 = 100;
		}

		#endregion
	}
}
